#pragma once
#include "Agreement.hpp"

#include <functional>

using cal_fun = std::function<Response(const Request &req)>;

class HandleMessage
{
public:
    HandleMessage(cal_fun cal)
        : _cal(cal)
    {
    }

    std::string Entry(std::string &package)
    {
        std::string ret;
        std::string message;
        while (SplitMessage(package, &message))
        {
            // 1.报文完整性

            // 2.反序列化
            Request req;
            req.Deserialization(message);

            // 3.进行计算
            Response res = _cal(req);

            // 4.对应答进行序列化
            std::string resmessage;
            res.Serialize(&resmessage);

            // 5.添加报头
            AddHeader(&resmessage);

            ret += resmessage;
        }

        return ret;
    }

    ~HandleMessage() = default;

private:
    cal_fun _cal;
};